package com.demo.system.init.sentinel;

import com.alibaba.csp.sentinel.adapter.dubbo.fallback.DubboFallbackRegistry;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.demo.system.init.sentinel.fallback.CustomerDefaultDubboFallback;
import com.demo.system.init.sentinel.handle.CustomerUrlBlockHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @author ：qilong sun
 * @date ：Created in 2020/7/21 13:22
 * @description：开启@SentinelResource注解支持
 * @modified By：
 * @version: V1.0$
 */
@Configuration
@Slf4j
public class SentinelConfiguration {

    static {
        // 服务端回调处理
        DubboFallbackRegistry.setProviderFallback(new CustomerDefaultDubboFallback());
    }

    /**
     * sentinel block 监控
     */
    @PostConstruct
    public void init(){
        // webCallback，在web端生效
         WebCallbackManager.setUrlBlockHandler(new CustomerUrlBlockHandler());
    }

    /**
     * 开启sentinel @SentinelResource注解
     *
     * @return
     */
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
}
